#include <stdio.h>
#include <ctype.h>
/* 3-1
 * (1) 折半查找函数
 */

int binsearch(int x, int v[], int n){

}

/* (2) 统计数字，只不过这次用的是switch，里面case的用法很经典
 *
 */

void wordCount(){
    int c, i, nwhite, nother, ndigit[10];

    nwhite = nother = 0;
    for (i = 0; i < 10; i++)
        ndigit[i] = 0;
    while(  (c = getchar()) != EOF){
        switch(c){
            case'0':case'1':case'2':case'3':case'4':
            case'5':case'6':case'7':case'8':case'9':
                ndigit[c-'0']++;
                break;
            case' ':case'\n':case'\t':
                nwhite++;
                break;
            default:
                nother++;
                break;
        }
    }
    printf("digits = ");
    for (i = 0; i < 10; i++)
        printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d", nwhite, nother);
}

/*
 * 经典面试题，字符串转数值，结合3-4，3-5，3-6改进
 */
int atoi (char s[]){

    int i, n, sign;
    for ( i = 0; isspace(s[i]); ++i)
        ;
    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-')
        i++;
    for (n=0; isdigit(s[i]); i++)
        n = 10 * n + (s[i] - '0');
    return sign * n;
}

/*
 *  3-3 expand 很有意思的字符串处理题目，很考验思维方式
 *  很巧妙的程序控制，s1[i++]等价的获取了s1[i]和i++
 */
void expand(char  s1[], char s2[]){
    char c;
    int i=0, j=0;

    while ((c = s1[i++]) != '\0' ){
        if (s1[i] == '-' && s1[i+1] >= c){
             i++;
             while(c < s1[i])
                 s2[j++] = c++;
        } else
            s2[j++] = c;
    }
    s2[j] = '\0';
}


/*
 *
 */
void itob (int n, char s[], int b){
    int i, j, sign;
    void reverse(char s[]);

    if ((sign = n) < 0)
        n = -n;
    i = 0;
    do {
        j = n % b;
        s[i++] = (j <= 9) ? j+'0' : j+'a'-10;
    } while ((n/=b) > 0);
    if (sign < 0)
        s[i++] = '-';
    s[i] = '\0';
    reverse(s);
}

int main() {


    return 0;
}
